import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/scheduler/ticker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_slidable_panel/controllers/slide_controller.dart';
import 'package:flutter_slidable_panel/flutter_slidable_panel.dart';
import 'package:flutter_slidable_panel/widgets/slidable_panel.dart';

import 'package:get/get.dart';
import 'package:wk_robot2/app/event/DataCallBack.dart';
import 'package:wk_robot2/app/utils/CommonUtil.dart';
import 'package:wk_robot2/app/widget/CommonWidgets.dart';

import '../../../models/Devices.dart';
import '../../../routes/app_pages.dart';
import '../../../style/app_colors.dart';
import '../../../widget/tips.dart';
import '../controllers/mine_robots_controller.dart';

class MineRobotsView extends GetView<MineRobotsController> {


   MineRobotsView({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(()=>Scaffold(
      backgroundColor: AppColors.device_page_bg_color,
      appBar: AppBar(
        title: const Text('我的机器人'),
        centerTitle: true,
        leading: IconButton(
          icon: Image.asset('assets/images/ic_back2.png',width: 48.w,height: 48.h),
          onPressed: () {
            Get.back();
          },
          tooltip: MaterialLocalizations.of(context).backButtonTooltip, // 可选：设置工具提示
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
        controller.myRobotList.length > 0 ? Container(
            height: 700.h,
            margin: EdgeInsets.all(40.h),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(16)),
                color: Colors.white
            ),
            child:  ListView.separated(itemBuilder: (context, index) {
              Device deviceItem = controller.myRobotList.value[index];


              return Container(
                padding: EdgeInsets.only(top: 22.h, bottom: 22.h,left: 30.w,right: 30.w),
                child: SlidablePanel(
                  controller: deviceItem.slideController!,
                  maxSlideThreshold: 0.2,
                  axis: Axis.horizontal,
                  gestureDisabled: true,
                  postActions: [
                    GestureDetector(
                      onTap: () {
                        deviceItem.slideController!.dismiss();
                        CommonWidgets.showConfirmDialog(context, '', '确定要解绑该机器人吗?', '解绑', '再想想', DataCallBack(
                          confirmCallBack: (v) {
                            controller.unBindRobote(deviceItem.robotUserId!);
                          },
                          cancleCallBack: (v) {

                          }
                        ));



                      },
                      child: Container(
                        color: Colors.red,
                        child: Center(
                          child: Text('解绑',style: TextStyle(color: Colors.white),
                        ),),
                      ),
                    )

                  ],
                  child: GestureDetector(
                    onTap: () {
                      if(deviceItem.slideController!.dismissed) {
                         deviceItem.slideController!.open(position:ActionPosition.post);
                      } else {
                        deviceItem.slideController!.dismiss();
                      }

                      controller.myRobotList.value.forEach((item){
                          if (item.robotUserId != deviceItem.robotUserId) {
                            item.slideController!.dismiss();
                          }
                      });

                    },
                    child: Container(
                      color: Colors.white.withOpacity(0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              Container(
                                child: Image.asset('assets/images/ic_avatar_online.png',width: 120.w,height: 120.h),
                              ),
                              SizedBox(width: 23.w),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    children: [
                                      Text('悟空机器人',style: TextStyle(fontSize: 32.sp),),
                                      SizedBox(width: 14.w,),
                                      Container(
                                        padding: EdgeInsets.only(left: 24.w,right: 23.w,top: 6.h,bottom: 6.h),
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(Radius.circular(20)),
                                            color: AppColors.robot_online_status_color
                                        ),
                                        child: Text(deviceItem.isOnline ? '在线':'离线',style: TextStyle(fontSize: 24.sp)),
                                      ),
                                      SizedBox(width: 16.w,),
                                      deviceItem.selected ? Container(
                                        padding: EdgeInsets.only(left: 24.w,right: 23.w,top: 6.h,bottom: 6.h),
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(Radius.circular(20)),
                                            color: AppColors.app_base_theme_color
                                        ),
                                        child: Text('当前',style: TextStyle(fontSize: 24.sp)),
                                      ): SizedBox()


                                    ],
                                  ),
                                  SizedBox(height: 12.h),
                                  Row(
                                    children: [
                                      Text(deviceItem.robotUserId!,style: TextStyle(color: AppColors.line_right_character_color),),
                                      SizedBox(width: 8.w,),
                                      GestureDetector(
                                        onTap: () {
                                          Clipboard.setData(ClipboardData(text: deviceItem.robotUserId!));
                                          Tips.showToast('已复制序列号');
                                        },
                                        child: Image.asset('assets/images/ic_copy.webp',width: 36.w,height: 36.h,)
                                      )

                                    ],
                                  )


                                ],
                              )

                            ],
                          )
                        ],
                      ),
                    ),
                  ),
                ),
              );

            }, separatorBuilder: (context, index) => Divider(color: AppColors.LIST_VIEW_SEPERATED_Color,height:2, thickness:0.5, indent: 6.0,endIndent: 6.0),
            itemCount: controller.myRobotList.value.length),
          ): Container(
                width: Get.width,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SizedBox(height: 200.h),
                    Image.asset('assets/images/mini_search_robot_founding.webp',width: 70.w,height: 130.h,),
                    Text('没有找到机器人')
                  ],
                ),
           ),
          SizedBox(height: 60.h),
          GestureDetector(
            onTap: () {
              // Get.toNamed(Routes.POWER_ONING);
              CommonUtil.bindDevices();
            },
            child: Container(
              width: 594.w,
              height: 88.h,
              child: Container(
                width: 260.w,
                height: 72.h,
                padding: EdgeInsets.only(top: 15.h,bottom: 15.h),
                decoration: BoxDecoration(
                  color: Colors.black,
                  borderRadius: BorderRadius.all(Radius.circular(90)),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset('assets/images/ic_bindding.webp',width: 48.w,height: 48.h),
                    SizedBox(width: 25.w,),
                    Text('添加机器人',style: TextStyle(fontSize: 30.sp, color: Colors.white))
                  ],
                ),
              ),
            ),
          )

        ],
      ),
    ));
  }


}
